{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "63396492-2899-4862-8cfb-84e812a844cc",
   "metadata": {},
   "source": [
    "# Waterfall Plot\n",
    "\n",
    "A waterfall plot shows the cumulative effect of sequentially introduced positive or negative values.\n",
    "\n",
    "To use it, you need to import the 'bistro' module."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "b1b75d26-0fde-4591-af27-677b5d807b5f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "   <div id=\"mIvAaY\"></div>\n",
       "   <script type=\"text/javascript\" data-lets-plot-script=\"library\">\n",
       "       if(!window.letsPlotCallQueue) {\n",
       "           window.letsPlotCallQueue = [];\n",
       "       }; \n",
       "       window.letsPlotCall = function(f) {\n",
       "           window.letsPlotCallQueue.push(f);\n",
       "       };\n",
       "       (function() {\n",
       "           var script = document.createElement(\"script\");\n",
       "           script.type = \"text/javascript\";\n",
       "           script.src = \"https://cdn.jsdelivr.net/gh/JetBrains/lets-plot@v4.4.0/js-package/distr/lets-plot.min.js\";\n",
       "           script.onload = function() {\n",
       "               window.letsPlotCall = function(f) {f();};\n",
       "               window.letsPlotCallQueue.forEach(function(f) {f();});\n",
       "               window.letsPlotCallQueue = [];\n",
       "               \n",
       "               \n",
       "           };\n",
       "           script.onerror = function(event) {\n",
       "               window.letsPlotCall = function(f) {};\n",
       "               window.letsPlotCallQueue = [];\n",
       "               var div = document.createElement(\"div\");\n",
       "               div.style.color = 'darkred';\n",
       "               div.textContent = 'Error loading Lets-Plot JS';\n",
       "               document.getElementById(\"mIvAaY\").appendChild(div);\n",
       "           };\n",
       "           var e = document.getElementById(\"mIvAaY\");\n",
       "           e.appendChild(script);\n",
       "       })();\n",
       "   </script>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "%useLatestDescriptors\n",
    "%use lets-plot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "7137dd20-92ce-4b54-87d1-a1088285f1d0",
   "metadata": {},
   "outputs": [],
   "source": [
    "val dataMap = mapOf(\n",
    "    \"Accounts\" to listOf(\"Product revenue\", \"Services revenue\", \"Fixed costs\", \"Variable costs\"),\n",
    "    \"Values\" to listOf(830_000, 290_000, -360_000, -150_000),\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1f861d4a-b5df-429e-b4e0-a2a925c4cb28",
   "metadata": {},
   "source": [
    "## Default View"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "201f8210-dfc1-4df2-b1eb-3e6c5cc1a92b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "   <div id=\"znEqXO\"></div>\n",
       "   <script type=\"text/javascript\" data-lets-plot-script=\"plot\">\n",
       "       (function() {\n",
       "           var plotSpec={\n",
       "\"mapping\":{\n",
       "},\n",
       "\"data\":{\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[{\n",
       "\"aesthetic\":\"x\",\n",
       "\"name\":\"Accounts\",\n",
       "\"breaks\":[0.0,1.0,2.0,3.0,4.0],\n",
       "\"labels\":[\"Product revenue\",\"Services revenue\",\"Fixed costs\",\"Variable costs\",\"Total\"]\n",
       "},{\n",
       "\"aesthetic\":\"y\",\n",
       "\"name\":\"Values\"\n",
       "},{\n",
       "\"aesthetic\":\"color\",\n",
       "\"name\":\"Flow type\",\n",
       "\"breaks\":[\"Increase\",\"Decrease\",\"Total\"],\n",
       "\"values\":[\"#4daf4a\",\"#e41a1c\",\"#377eb8\"]\n",
       "},{\n",
       "\"aesthetic\":\"fill\",\n",
       "\"name\":\"Flow type\",\n",
       "\"breaks\":[\"Increase\",\"Decrease\",\"Total\"],\n",
       "\"values\":[\"#4daf4a\",\"#e41a1c\",\"#377eb8\"]\n",
       "}],\n",
       "\"layers\":[{\n",
       "\"geom\":\"spoke\",\n",
       "\"data\":{\n",
       "\"..radius..\":[0.09999999999999998,0.09999999999999998,0.09999999999999998,0.09999999999999998,0.0],\n",
       "\"..x..\":[0.0,1.0,2.0,3.0,4.0],\n",
       "\"..y..\":[830000.0,1120000.0,760000.0,610000.0,610000.0]\n",
       "},\n",
       "\"mapping\":{\n",
       "\"x\":\"..x..\",\n",
       "\"y\":\"..y..\",\n",
       "\"radius\":\"..radius..\"\n",
       "},\n",
       "\"angle\":0.0,\n",
       "\"position\":{\n",
       "\"name\":\"nudge\",\n",
       "\"x\":0.45\n",
       "}\n",
       "},{\n",
       "\"geom\":\"crossbar\",\n",
       "\"data\":{\n",
       "\"..flow_type..\":[\"Increase\",\"Increase\",\"Decrease\",\"Decrease\",\"Total\"],\n",
       "\"..dy..\":[830000.0,290000.0,-360000.0,-150000.0,610000.0],\n",
       "\"..x..\":[0.0,1.0,2.0,3.0,4.0],\n",
       "\"..ymax..\":[830000.0,1120000.0,1120000.0,760000.0,null],\n",
       "\"..ymin..\":[0.0,830000.0,760000.0,610000.0,null]\n",
       "},\n",
       "\"mapping\":{\n",
       "\"x\":\"..x..\",\n",
       "\"ymin\":\"..ymin..\",\n",
       "\"ymax\":\"..ymax..\",\n",
       "\"fill\":\"..flow_type..\"\n",
       "},\n",
       "\"color\":\"black\",\n",
       "\"size\":0.0,\n",
       "\"linetype\":1,\n",
       "\"width\":0.9,\n",
       "\"show_legend\":false,\n",
       "\"tooltips\":{\n",
       "\"lines\":[\"@..dy..\"],\n",
       "\"formats\":[{\n",
       "\"field\":\"..dy..\",\n",
       "\"format\":\".2~f\"\n",
       "}]\n",
       "}\n",
       "},{\n",
       "\"geom\":\"crossbar\",\n",
       "\"data\":{\n",
       "\"..value..\":[830000.0,1120000.0,760000.0,610000.0,610000.0],\n",
       "\"..flow_type..\":[\"Increase\",\"Increase\",\"Decrease\",\"Decrease\",\"Total\"],\n",
       "\"..x..\":[0.0,1.0,2.0,3.0,4.0],\n",
       "\"..ymax..\":[null,null,null,null,610000.0],\n",
       "\"..ymin..\":[null,null,null,null,0.0]\n",
       "},\n",
       "\"mapping\":{\n",
       "\"x\":\"..x..\",\n",
       "\"ymin\":\"..ymin..\",\n",
       "\"ymax\":\"..ymax..\",\n",
       "\"fill\":\"..flow_type..\"\n",
       "},\n",
       "\"color\":\"black\",\n",
       "\"size\":0.0,\n",
       "\"linetype\":1,\n",
       "\"width\":0.9,\n",
       "\"show_legend\":false,\n",
       "\"tooltips\":{\n",
       "\"disable_splitting\":true,\n",
       "\"lines\":[\"@..value..\"],\n",
       "\"formats\":[{\n",
       "\"field\":\"..value..\",\n",
       "\"format\":\".2~f\"\n",
       "}]\n",
       "}\n",
       "},{\n",
       "\"geom\":\"text\",\n",
       "\"data\":{\n",
       "\"..x..\":[0.0,1.0,2.0,3.0,4.0],\n",
       "\"..y..\":[415000.0,975000.0,940000.0,685000.0,305000.0],\n",
       "\"..label..\":[830000.0,290000.0,-360000.0,-150000.0,610000.0]\n",
       "},\n",
       "\"mapping\":{\n",
       "\"x\":\"..x..\",\n",
       "\"y\":\"..y..\",\n",
       "\"label\":\"..label..\"\n",
       "},\n",
       "\"color\":\"white\",\n",
       "\"show_legend\":false,\n",
       "\"label_format\":\".2~f\"\n",
       "}],\n",
       "\"data_meta\":{\n",
       "\"series_annotations\":[{\n",
       "\"type\":\"str\",\n",
       "\"column\":\"Accounts\"\n",
       "},{\n",
       "\"type\":\"int\",\n",
       "\"column\":\"Values\"\n",
       "}]\n",
       "},\n",
       "\"theme\":{\n",
       "\"axis_tooltip\":\"blank\"\n",
       "}\n",
       "};\n",
       "           var plotContainer = document.getElementById(\"znEqXO\");\n",
       "           window.letsPlotCall(function() {{\n",
       "               LetsPlot.buildPlotFromProcessedSpecs(plotSpec, -1, -1, plotContainer);\n",
       "           }});\n",
       "       })();\n",
       "   </script>"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "waterfallPlot(dataMap, \"Accounts\", \"Values\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e3de1d7d-641f-4403-befe-2846b74e4368",
   "metadata": {},
   "source": [
    "## Improved View"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "cfaa985d-f86a-407c-a1be-b545201c2492",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "   <div id=\"QVyMQ0\"></div>\n",
       "   <script type=\"text/javascript\" data-lets-plot-script=\"plot\">\n",
       "       (function() {\n",
       "           var plotSpec={\n",
       "\"ggtitle\":{\n",
       "\"text\":\"Company Profit (in USD)\"\n",
       "},\n",
       "\"mapping\":{\n",
       "},\n",
       "\"data\":{\n",
       "},\n",
       "\"ggsize\":{\n",
       "\"width\":1000.0,\n",
       "\"height\":500.0\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[{\n",
       "\"aesthetic\":\"x\",\n",
       "\"name\":\"Accounts\",\n",
       "\"breaks\":[0.0,1.0,2.0,3.0,4.0],\n",
       "\"labels\":[\"Product revenue\",\"Services revenue\",\"Fixed costs\",\"Variable costs\",\"Profit\"]\n",
       "},{\n",
       "\"aesthetic\":\"y\",\n",
       "\"name\":\"Values\"\n",
       "},{\n",
       "\"aesthetic\":\"color\",\n",
       "\"name\":\"Flow type\",\n",
       "\"breaks\":[\"Increase\",\"Decrease\",\"Profit\"],\n",
       "\"values\":[\"#4daf4a\",\"#e41a1c\",\"#377eb8\"]\n",
       "},{\n",
       "\"aesthetic\":\"fill\",\n",
       "\"name\":\"Flow type\",\n",
       "\"breaks\":[\"Increase\",\"Decrease\",\"Profit\"],\n",
       "\"values\":[\"#4daf4a\",\"#e41a1c\",\"#377eb8\"]\n",
       "},{\n",
       "\"aesthetic\":\"y\",\n",
       "\"name\":\"Values\",\n",
       "\"format\":\"$,.1~s\"\n",
       "}],\n",
       "\"layers\":[{\n",
       "\"geom\":\"spoke\",\n",
       "\"data\":{\n",
       "\"..radius..\":[0.19999999999999996,0.19999999999999996,0.19999999999999996,0.19999999999999996,0.0],\n",
       "\"..x..\":[0.0,1.0,2.0,3.0,4.0],\n",
       "\"..y..\":[830000.0,1120000.0,760000.0,610000.0,610000.0]\n",
       "},\n",
       "\"mapping\":{\n",
       "\"x\":\"..x..\",\n",
       "\"y\":\"..y..\",\n",
       "\"radius\":\"..radius..\"\n",
       "},\n",
       "\"angle\":0.0,\n",
       "\"position\":{\n",
       "\"name\":\"nudge\",\n",
       "\"x\":0.4\n",
       "},\n",
       "\"linetype\":3\n",
       "},{\n",
       "\"geom\":\"crossbar\",\n",
       "\"data\":{\n",
       "\"..flow_type..\":[\"Increase\",\"Increase\",\"Decrease\",\"Decrease\",\"Profit\"],\n",
       "\"..dy..\":[830000.0,290000.0,-360000.0,-150000.0,610000.0],\n",
       "\"..x..\":[0.0,1.0,2.0,3.0,4.0],\n",
       "\"..ymax..\":[830000.0,1120000.0,1120000.0,760000.0,null],\n",
       "\"..ymin..\":[0.0,830000.0,760000.0,610000.0,null]\n",
       "},\n",
       "\"mapping\":{\n",
       "\"x\":\"..x..\",\n",
       "\"ymin\":\"..ymin..\",\n",
       "\"ymax\":\"..ymax..\",\n",
       "\"fill\":\"..flow_type..\"\n",
       "},\n",
       "\"color\":\"black\",\n",
       "\"size\":0.75,\n",
       "\"alpha\":0.15,\n",
       "\"linetype\":1,\n",
       "\"width\":0.8,\n",
       "\"show_legend\":false,\n",
       "\"tooltips\":{\n",
       "\"lines\":[\"@..dy..\"],\n",
       "\"formats\":[{\n",
       "\"field\":\"..dy..\",\n",
       "\"format\":\".2~f\"\n",
       "}]\n",
       "}\n",
       "},{\n",
       "\"geom\":\"crossbar\",\n",
       "\"data\":{\n",
       "\"..value..\":[830000.0,1120000.0,760000.0,610000.0,610000.0],\n",
       "\"..flow_type..\":[\"Increase\",\"Increase\",\"Decrease\",\"Decrease\",\"Profit\"],\n",
       "\"..x..\":[0.0,1.0,2.0,3.0,4.0],\n",
       "\"..ymax..\":[null,null,null,null,610000.0],\n",
       "\"..ymin..\":[null,null,null,null,0.0]\n",
       "},\n",
       "\"mapping\":{\n",
       "\"x\":\"..x..\",\n",
       "\"ymin\":\"..ymin..\",\n",
       "\"ymax\":\"..ymax..\",\n",
       "\"fill\":\"..flow_type..\"\n",
       "},\n",
       "\"color\":\"black\",\n",
       "\"size\":0.75,\n",
       "\"alpha\":0.15,\n",
       "\"linetype\":1,\n",
       "\"width\":0.8,\n",
       "\"show_legend\":false,\n",
       "\"tooltips\":{\n",
       "\"disable_splitting\":true,\n",
       "\"lines\":[\"@..value..\"],\n",
       "\"formats\":[{\n",
       "\"field\":\"..value..\",\n",
       "\"format\":\".2~f\"\n",
       "}]\n",
       "}\n",
       "},{\n",
       "\"geom\":\"text\",\n",
       "\"data\":{\n",
       "\"..flow_type..\":[\"Increase\",\"Increase\",\"Decrease\",\"Decrease\",\"Profit\"],\n",
       "\"..x..\":[0.0,1.0,2.0,3.0,4.0],\n",
       "\"..y..\":[415000.0,975000.0,940000.0,685000.0,305000.0],\n",
       "\"..label..\":[830000.0,290000.0,-360000.0,-150000.0,610000.0]\n",
       "},\n",
       "\"mapping\":{\n",
       "\"x\":\"..x..\",\n",
       "\"y\":\"..y..\",\n",
       "\"label\":\"..label..\",\n",
       "\"color\":\"..flow_type..\"\n",
       "},\n",
       "\"family\":\"Courier\",\n",
       "\"fontface\":\"bold\",\n",
       "\"size\":10.0,\n",
       "\"show_legend\":false,\n",
       "\"label_format\":\"$,.1~s\"\n",
       "},{\n",
       "\"geom\":\"hline\",\n",
       "\"yintercept\":0.0,\n",
       "\"size\":1.0,\n",
       "\"linetype\":1,\n",
       "\"tooltips\":\"none\",\n",
       "\"data\":{\n",
       "}\n",
       "}],\n",
       "\"theme\":{\n",
       "\"axis_tooltip\":\"blank\",\n",
       "\"name\":\"minimal\",\n",
       "\"plot_title\":{\n",
       "\"face\":\"bold\",\n",
       "\"size\":20.0,\n",
       "\"hjust\":0.5,\n",
       "\"blank\":false\n",
       "}\n",
       "},\n",
       "\"data_meta\":{\n",
       "\"series_annotations\":[{\n",
       "\"type\":\"str\",\n",
       "\"column\":\"Accounts\"\n",
       "},{\n",
       "\"type\":\"int\",\n",
       "\"column\":\"Values\"\n",
       "}]\n",
       "}\n",
       "};\n",
       "           var plotContainer = document.getElementById(\"QVyMQ0\");\n",
       "           window.letsPlotCall(function() {{\n",
       "               LetsPlot.buildPlotFromProcessedSpecs(plotSpec, -1, -1, plotContainer);\n",
       "           }});\n",
       "       })();\n",
       "   </script>"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "waterfallPlot(dataMap, \"Accounts\", \"Values\",\n",
    "              size = 0.75, alpha = 0.15, width = 0.8, totalTitle = \"Profit\",\n",
    "              hline = elementLine(linetype = \"solid\", size = 1.0),\n",
    "              connector = elementLine(linetype = \"dotted\"),\n",
    "              label = elementText(size = 10.0, family = \"Courier\", face = \"bold\", color = \"flow_type\"),\n",
    "              labelFormat = \"$,.1~s\") +\n",
    "    scaleYContinuous(name = \"Values\", format = \"$,.1~s\") +\n",
    "    ggtitle(\"Company Profit (in USD)\") +\n",
    "    ggsize(1000, 500) +\n",
    "    themeMinimal() +\n",
    "    theme(plotTitle = elementText(size = 20.0, face = \"bold\", hjust = 0.5))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5ea2b8fb-b0f1-4183-966a-ddc10d0253c6",
   "metadata": {},
   "source": [
    "## Additional Parameters"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9dd5211f-3797-4c3c-a51d-515c48d3eff3",
   "metadata": {},
   "source": [
    "### `measure` and `group`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "b20767fb-fa6e-4dfe-b186-49706ff1a757",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "   <div id=\"lCDTCV\"></div>\n",
       "   <script type=\"text/javascript\" data-lets-plot-script=\"plot\">\n",
       "       (function() {\n",
       "           var plotSpec={\n",
       "\"mapping\":{\n",
       "},\n",
       "\"data\":{\n",
       "\"Company\":[\"Badgersoft\",\"Badgersoft\",\"Badgersoft\",\"Badgersoft\",\"Badgersoft\",\"Badgersoft\",\"Badgersoft\",\"AIlien Co.\",\"AIlien Co.\",\"AIlien Co.\",\"AIlien Co.\",\"AIlien Co.\",\"AIlien Co.\",\"AIlien Co.\"]\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[{\n",
       "\"aesthetic\":\"x\",\n",
       "\"name\":\"Accounts\",\n",
       "\"breaks\":[0.0,1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0,11.0,12.0,13.0],\n",
       "\"labels\":[\"initial\",\"revenue\",\"costs\",\"Q1\",\"revenue\",\"costs\",\"Q2\",\"initial\",\"revenue\",\"costs\",\"Q1\",\"revenue\",\"costs\",\"Q2\"]\n",
       "},{\n",
       "\"aesthetic\":\"y\",\n",
       "\"name\":\"Values\"\n",
       "},{\n",
       "\"aesthetic\":\"color\",\n",
       "\"name\":\"Flow type\",\n",
       "\"breaks\":[\"Increase\",\"Decrease\",\"Absolute\",\"Total\"],\n",
       "\"values\":[\"#4daf4a\",\"#e41a1c\",\"#377eb8\",\"#377eb8\"]\n",
       "},{\n",
       "\"aesthetic\":\"fill\",\n",
       "\"name\":\"Flow type\",\n",
       "\"breaks\":[\"Increase\",\"Decrease\",\"Absolute\",\"Total\"],\n",
       "\"values\":[\"#4daf4a\",\"#e41a1c\",\"#377eb8\",\"#377eb8\"]\n",
       "}],\n",
       "\"layers\":[{\n",
       "\"geom\":\"spoke\",\n",
       "\"data\":{\n",
       "\"..radius..\":[0.09999999999999998,0.09999999999999998,0.09999999999999998,0.09999999999999998,0.09999999999999998,0.09999999999999998,0.0,0.09999999999999998,0.09999999999999998,0.09999999999999998,0.09999999999999998,0.09999999999999998,0.09999999999999998,0.0],\n",
       "\"..x..\":[0.0,1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0,11.0,12.0,13.0],\n",
       "\"..y..\":[200.0,400.0,300.0,300.0,550.0,450.0,450.0,150.0,200.0,100.0,100.0,200.0,100.0,100.0]\n",
       "},\n",
       "\"mapping\":{\n",
       "\"x\":\"..x..\",\n",
       "\"y\":\"..y..\",\n",
       "\"radius\":\"..radius..\"\n",
       "},\n",
       "\"angle\":0.0,\n",
       "\"position\":{\n",
       "\"name\":\"nudge\",\n",
       "\"x\":0.45\n",
       "}\n",
       "},{\n",
       "\"geom\":\"crossbar\",\n",
       "\"data\":{\n",
       "\"..flow_type..\":[\"Absolute\",\"Increase\",\"Decrease\",\"Total\",\"Increase\",\"Decrease\",\"Total\",\"Absolute\",\"Increase\",\"Decrease\",\"Total\",\"Increase\",\"Decrease\",\"Total\"],\n",
       "\"..dy..\":[200.0,200.0,-100.0,300.0,250.0,-100.0,150.0,150.0,50.0,-100.0,100.0,100.0,-100.0,0.0],\n",
       "\"..x..\":[0.0,1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0,11.0,12.0,13.0],\n",
       "\"..ymax..\":[null,400.0,400.0,null,550.0,550.0,null,null,200.0,200.0,null,200.0,200.0,null],\n",
       "\"..ymin..\":[null,200.0,300.0,null,300.0,450.0,null,null,150.0,100.0,null,100.0,100.0,null]\n",
       "},\n",
       "\"mapping\":{\n",
       "\"x\":\"..x..\",\n",
       "\"ymin\":\"..ymin..\",\n",
       "\"ymax\":\"..ymax..\",\n",
       "\"fill\":\"..flow_type..\"\n",
       "},\n",
       "\"color\":\"black\",\n",
       "\"size\":0.0,\n",
       "\"linetype\":1,\n",
       "\"width\":0.9,\n",
       "\"show_legend\":false,\n",
       "\"tooltips\":{\n",
       "\"lines\":[\"@..dy..\"],\n",
       "\"formats\":[{\n",
       "\"field\":\"..dy..\",\n",
       "\"format\":\".2~f\"\n",
       "}]\n",
       "}\n",
       "},{\n",
       "\"geom\":\"crossbar\",\n",
       "\"data\":{\n",
       "\"..value..\":[200.0,400.0,300.0,300.0,550.0,450.0,450.0,150.0,200.0,100.0,100.0,200.0,100.0,100.0],\n",
       "\"..flow_type..\":[\"Absolute\",\"Increase\",\"Decrease\",\"Total\",\"Increase\",\"Decrease\",\"Total\",\"Absolute\",\"Increase\",\"Decrease\",\"Total\",\"Increase\",\"Decrease\",\"Total\"],\n",
       "\"..x..\":[0.0,1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0,11.0,12.0,13.0],\n",
       "\"..ymax..\":[200.0,null,null,300.0,null,null,450.0,150.0,null,null,100.0,null,null,100.0],\n",
       "\"..ymin..\":[0.0,null,null,0.0,null,null,0.0,0.0,null,null,0.0,null,null,0.0]\n",
       "},\n",
       "\"mapping\":{\n",
       "\"x\":\"..x..\",\n",
       "\"ymin\":\"..ymin..\",\n",
       "\"ymax\":\"..ymax..\",\n",
       "\"fill\":\"..flow_type..\"\n",
       "},\n",
       "\"color\":\"black\",\n",
       "\"size\":0.0,\n",
       "\"linetype\":1,\n",
       "\"width\":0.9,\n",
       "\"show_legend\":false,\n",
       "\"tooltips\":{\n",
       "\"disable_splitting\":true,\n",
       "\"lines\":[\"@..value..\"],\n",
       "\"formats\":[{\n",
       "\"field\":\"..value..\",\n",
       "\"format\":\".2~f\"\n",
       "}]\n",
       "}\n",
       "},{\n",
       "\"geom\":\"text\",\n",
       "\"data\":{\n",
       "\"..x..\":[0.0,1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0,11.0,12.0,13.0],\n",
       "\"..y..\":[100.0,300.0,350.0,150.0,425.0,500.0,225.0,75.0,175.0,150.0,50.0,150.0,150.0,50.0],\n",
       "\"..label..\":[200.0,200.0,-100.0,300.0,250.0,-100.0,450.0,150.0,50.0,-100.0,100.0,100.0,-100.0,100.0]\n",
       "},\n",
       "\"mapping\":{\n",
       "\"x\":\"..x..\",\n",
       "\"y\":\"..y..\",\n",
       "\"label\":\"..label..\"\n",
       "},\n",
       "\"color\":\"white\",\n",
       "\"show_legend\":false,\n",
       "\"label_format\":\".2~f\"\n",
       "}],\n",
       "\"facet\":{\n",
       "\"name\":\"grid\",\n",
       "\"x\":\"Company\",\n",
       "\"x_order\":1.0,\n",
       "\"scales\":\"free_x\",\n",
       "\"y_order\":1.0\n",
       "},\n",
       "\"data_meta\":{\n",
       "\"series_annotations\":[{\n",
       "\"type\":\"str\",\n",
       "\"column\":\"Company\"\n",
       "},{\n",
       "\"type\":\"str\",\n",
       "\"column\":\"Accounts\"\n",
       "},{\n",
       "\"type\":\"int\",\n",
       "\"column\":\"Values\"\n",
       "},{\n",
       "\"type\":\"str\",\n",
       "\"column\":\"Measure\"\n",
       "}]\n",
       "},\n",
       "\"theme\":{\n",
       "\"axis_tooltip\":\"blank\"\n",
       "}\n",
       "};\n",
       "           var plotContainer = document.getElementById(\"lCDTCV\");\n",
       "           window.letsPlotCall(function() {{\n",
       "               LetsPlot.buildPlotFromProcessedSpecs(plotSpec, -1, -1, plotContainer);\n",
       "           }});\n",
       "       })();\n",
       "   </script>"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "val groupSize = 7\n",
    "val dataWithGroupsMap = mapOf(\n",
    "    \"Company\" to List(groupSize) { \"Badgersoft\" } + List(groupSize) { \"AIlien Co.\" },\n",
    "    \"Accounts\" to listOf(\"initial\", \"revenue\", \"costs\", \"Q1\", \"revenue\", \"costs\", \"Q2\").let { it + it },\n",
    "    \"Values\" to listOf(200, 200, -100, null, 250, -100, null,\n",
    "                       150, 50, -100, null, 100, -100, null),\n",
    "    \"Measure\" to listOf(\"absolute\", \"relative\", \"relative\", \"total\", \"relative\", \"relative\", \"total\").let { it + it },\n",
    ")\n",
    "\n",
    "waterfallPlot(dataWithGroupsMap, \"Accounts\", \"Values\", measure = \"Measure\", group = \"Company\") +\n",
    "    facetGrid(x = \"Company\", scales = \"free_x\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ea6ca346-cec9-4cd6-ae8d-698b9c7375e7",
   "metadata": {},
   "source": [
    "### `calcTotal`\n",
    "\n",
    "`calcTotal = false` disables the calculation of the total.\n",
    "\n",
    "If the `measure` serie is specified however, the `calcTotal` setting has no effect."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "5559c152-b59f-45b3-a12d-2a3266d8d6d3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "   <div id=\"qiZjtf\"></div>\n",
       "   <script type=\"text/javascript\" data-lets-plot-script=\"plot\">\n",
       "       (function() {\n",
       "           var plotSpec={\n",
       "\"layout\":{\n",
       "\"name\":\"grid\",\n",
       "\"ncol\":2,\n",
       "\"nrow\":1,\n",
       "\"fit\":true,\n",
       "\"align\":false\n",
       "},\n",
       "\"figures\":[{\n",
       "\"mapping\":{\n",
       "},\n",
       "\"data\":{\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[{\n",
       "\"aesthetic\":\"x\",\n",
       "\"name\":\"Accounts\",\n",
       "\"breaks\":[0.0,1.0,2.0,3.0],\n",
       "\"labels\":[\"Product revenue\",\"Services revenue\",\"Fixed costs\",\"Variable costs\"]\n",
       "},{\n",
       "\"aesthetic\":\"y\",\n",
       "\"name\":\"Values\"\n",
       "},{\n",
       "\"aesthetic\":\"color\",\n",
       "\"name\":\"Flow type\",\n",
       "\"breaks\":[\"Increase\",\"Decrease\"],\n",
       "\"values\":[\"#4daf4a\",\"#e41a1c\"]\n",
       "},{\n",
       "\"aesthetic\":\"fill\",\n",
       "\"name\":\"Flow type\",\n",
       "\"breaks\":[\"Increase\",\"Decrease\"],\n",
       "\"values\":[\"#4daf4a\",\"#e41a1c\"]\n",
       "}],\n",
       "\"layers\":[{\n",
       "\"geom\":\"spoke\",\n",
       "\"data\":{\n",
       "\"..radius..\":[0.09999999999999998,0.09999999999999998,0.09999999999999998,0.0],\n",
       "\"..x..\":[0.0,1.0,2.0,3.0],\n",
       "\"..y..\":[830000.0,1120000.0,760000.0,610000.0]\n",
       "},\n",
       "\"mapping\":{\n",
       "\"x\":\"..x..\",\n",
       "\"y\":\"..y..\",\n",
       "\"radius\":\"..radius..\"\n",
       "},\n",
       "\"angle\":0.0,\n",
       "\"position\":{\n",
       "\"name\":\"nudge\",\n",
       "\"x\":0.45\n",
       "}\n",
       "},{\n",
       "\"geom\":\"crossbar\",\n",
       "\"data\":{\n",
       "\"..flow_type..\":[\"Increase\",\"Increase\",\"Decrease\",\"Decrease\"],\n",
       "\"..dy..\":[830000.0,290000.0,-360000.0,-150000.0],\n",
       "\"..x..\":[0.0,1.0,2.0,3.0],\n",
       "\"..ymax..\":[830000.0,1120000.0,1120000.0,760000.0],\n",
       "\"..ymin..\":[0.0,830000.0,760000.0,610000.0]\n",
       "},\n",
       "\"mapping\":{\n",
       "\"x\":\"..x..\",\n",
       "\"ymin\":\"..ymin..\",\n",
       "\"ymax\":\"..ymax..\",\n",
       "\"fill\":\"..flow_type..\"\n",
       "},\n",
       "\"color\":\"black\",\n",
       "\"size\":0.0,\n",
       "\"linetype\":1,\n",
       "\"width\":0.9,\n",
       "\"show_legend\":false,\n",
       "\"tooltips\":{\n",
       "\"lines\":[\"@..dy..\"],\n",
       "\"formats\":[{\n",
       "\"field\":\"..dy..\",\n",
       "\"format\":\".2~f\"\n",
       "}]\n",
       "}\n",
       "},{\n",
       "\"geom\":\"crossbar\",\n",
       "\"data\":{\n",
       "\"..value..\":[830000.0,1120000.0,760000.0,610000.0],\n",
       "\"..flow_type..\":[\"Increase\",\"Increase\",\"Decrease\",\"Decrease\"],\n",
       "\"..x..\":[0.0,1.0,2.0,3.0],\n",
       "\"..ymax..\":[null,null,null,null],\n",
       "\"..ymin..\":[null,null,null,null]\n",
       "},\n",
       "\"mapping\":{\n",
       "\"x\":\"..x..\",\n",
       "\"ymin\":\"..ymin..\",\n",
       "\"ymax\":\"..ymax..\",\n",
       "\"fill\":\"..flow_type..\"\n",
       "},\n",
       "\"color\":\"black\",\n",
       "\"size\":0.0,\n",
       "\"linetype\":1,\n",
       "\"width\":0.9,\n",
       "\"show_legend\":false,\n",
       "\"tooltips\":{\n",
       "\"disable_splitting\":true,\n",
       "\"lines\":[\"@..value..\"],\n",
       "\"formats\":[{\n",
       "\"field\":\"..value..\",\n",
       "\"format\":\".2~f\"\n",
       "}]\n",
       "}\n",
       "},{\n",
       "\"geom\":\"text\",\n",
       "\"data\":{\n",
       "\"..x..\":[0.0,1.0,2.0,3.0],\n",
       "\"..y..\":[415000.0,975000.0,940000.0,685000.0],\n",
       "\"..label..\":[830000.0,290000.0,-360000.0,-150000.0]\n",
       "},\n",
       "\"mapping\":{\n",
       "\"x\":\"..x..\",\n",
       "\"y\":\"..y..\",\n",
       "\"label\":\"..label..\"\n",
       "},\n",
       "\"color\":\"white\",\n",
       "\"show_legend\":false,\n",
       "\"label_format\":\".2~f\"\n",
       "}],\n",
       "\"data_meta\":{\n",
       "\"series_annotations\":[{\n",
       "\"type\":\"str\",\n",
       "\"column\":\"Accounts\"\n",
       "},{\n",
       "\"type\":\"int\",\n",
       "\"column\":\"Values\"\n",
       "}]\n",
       "},\n",
       "\"theme\":{\n",
       "\"axis_tooltip\":\"blank\"\n",
       "}\n",
       "},{\n",
       "\"mapping\":{\n",
       "},\n",
       "\"data\":{\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[{\n",
       "\"aesthetic\":\"x\",\n",
       "\"name\":\"Accounts\",\n",
       "\"breaks\":[0.0,1.0,2.0,3.0,4.0,5.0,6.0],\n",
       "\"labels\":[\"initial\",\"revenue\",\"costs\",\"Q1\",\"revenue\",\"costs\",\"Q2\"]\n",
       "},{\n",
       "\"aesthetic\":\"y\",\n",
       "\"name\":\"Values\"\n",
       "},{\n",
       "\"aesthetic\":\"color\",\n",
       "\"name\":\"Flow type\",\n",
       "\"breaks\":[\"Increase\",\"Decrease\",\"Absolute\",\"Total\"],\n",
       "\"values\":[\"#4daf4a\",\"#e41a1c\",\"#377eb8\",\"#377eb8\"]\n",
       "},{\n",
       "\"aesthetic\":\"fill\",\n",
       "\"name\":\"Flow type\",\n",
       "\"breaks\":[\"Increase\",\"Decrease\",\"Absolute\",\"Total\"],\n",
       "\"values\":[\"#4daf4a\",\"#e41a1c\",\"#377eb8\",\"#377eb8\"]\n",
       "}],\n",
       "\"layers\":[{\n",
       "\"geom\":\"spoke\",\n",
       "\"data\":{\n",
       "\"..radius..\":[0.09999999999999998,0.09999999999999998,0.09999999999999998,0.09999999999999998,0.09999999999999998,0.09999999999999998,0.0],\n",
       "\"..x..\":[0.0,1.0,2.0,3.0,4.0,5.0,6.0],\n",
       "\"..y..\":[200.0,400.0,300.0,300.0,550.0,450.0,450.0]\n",
       "},\n",
       "\"mapping\":{\n",
       "\"x\":\"..x..\",\n",
       "\"y\":\"..y..\",\n",
       "\"radius\":\"..radius..\"\n",
       "},\n",
       "\"angle\":0.0,\n",
       "\"position\":{\n",
       "\"name\":\"nudge\",\n",
       "\"x\":0.45\n",
       "}\n",
       "},{\n",
       "\"geom\":\"crossbar\",\n",
       "\"data\":{\n",
       "\"..flow_type..\":[\"Absolute\",\"Increase\",\"Decrease\",\"Total\",\"Increase\",\"Decrease\",\"Total\"],\n",
       "\"..dy..\":[200.0,200.0,-100.0,300.0,250.0,-100.0,150.0],\n",
       "\"..x..\":[0.0,1.0,2.0,3.0,4.0,5.0,6.0],\n",
       "\"..ymax..\":[null,400.0,400.0,null,550.0,550.0,null],\n",
       "\"..ymin..\":[null,200.0,300.0,null,300.0,450.0,null]\n",
       "},\n",
       "\"mapping\":{\n",
       "\"x\":\"..x..\",\n",
       "\"ymin\":\"..ymin..\",\n",
       "\"ymax\":\"..ymax..\",\n",
       "\"fill\":\"..flow_type..\"\n",
       "},\n",
       "\"color\":\"black\",\n",
       "\"size\":0.0,\n",
       "\"linetype\":1,\n",
       "\"width\":0.9,\n",
       "\"show_legend\":false,\n",
       "\"tooltips\":{\n",
       "\"lines\":[\"@..dy..\"],\n",
       "\"formats\":[{\n",
       "\"field\":\"..dy..\",\n",
       "\"format\":\".2~f\"\n",
       "}]\n",
       "}\n",
       "},{\n",
       "\"geom\":\"crossbar\",\n",
       "\"data\":{\n",
       "\"..value..\":[200.0,400.0,300.0,300.0,550.0,450.0,450.0],\n",
       "\"..flow_type..\":[\"Absolute\",\"Increase\",\"Decrease\",\"Total\",\"Increase\",\"Decrease\",\"Total\"],\n",
       "\"..x..\":[0.0,1.0,2.0,3.0,4.0,5.0,6.0],\n",
       "\"..ymax..\":[200.0,null,null,300.0,null,null,450.0],\n",
       "\"..ymin..\":[0.0,null,null,0.0,null,null,0.0]\n",
       "},\n",
       "\"mapping\":{\n",
       "\"x\":\"..x..\",\n",
       "\"ymin\":\"..ymin..\",\n",
       "\"ymax\":\"..ymax..\",\n",
       "\"fill\":\"..flow_type..\"\n",
       "},\n",
       "\"color\":\"black\",\n",
       "\"size\":0.0,\n",
       "\"linetype\":1,\n",
       "\"width\":0.9,\n",
       "\"show_legend\":false,\n",
       "\"tooltips\":{\n",
       "\"disable_splitting\":true,\n",
       "\"lines\":[\"@..value..\"],\n",
       "\"formats\":[{\n",
       "\"field\":\"..value..\",\n",
       "\"format\":\".2~f\"\n",
       "}]\n",
       "}\n",
       "},{\n",
       "\"geom\":\"text\",\n",
       "\"data\":{\n",
       "\"..x..\":[0.0,1.0,2.0,3.0,4.0,5.0,6.0],\n",
       "\"..y..\":[100.0,300.0,350.0,150.0,425.0,500.0,225.0],\n",
       "\"..label..\":[200.0,200.0,-100.0,300.0,250.0,-100.0,450.0]\n",
       "},\n",
       "\"mapping\":{\n",
       "\"x\":\"..x..\",\n",
       "\"y\":\"..y..\",\n",
       "\"label\":\"..label..\"\n",
       "},\n",
       "\"color\":\"white\",\n",
       "\"show_legend\":false,\n",
       "\"label_format\":\".2~f\"\n",
       "}],\n",
       "\"data_meta\":{\n",
       "\"series_annotations\":[{\n",
       "\"type\":\"str\",\n",
       "\"column\":\"Company\"\n",
       "},{\n",
       "\"type\":\"str\",\n",
       "\"column\":\"Accounts\"\n",
       "},{\n",
       "\"type\":\"int\",\n",
       "\"column\":\"Values\"\n",
       "},{\n",
       "\"type\":\"str\",\n",
       "\"column\":\"Measure\"\n",
       "}]\n",
       "},\n",
       "\"theme\":{\n",
       "\"axis_tooltip\":\"blank\"\n",
       "}\n",
       "}],\n",
       "\"kind\":\"subplots\"\n",
       "};\n",
       "           var plotContainer = document.getElementById(\"qiZjtf\");\n",
       "           window.letsPlotCall(function() {{\n",
       "               LetsPlot.buildPlotFromProcessedSpecs(plotSpec, -1, -1, plotContainer);\n",
       "           }});\n",
       "       })();\n",
       "   </script>"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gggrid(listOf(\n",
    "    waterfallPlot(dataMap, \"Accounts\", \"Values\", calcTotal = false),\n",
    "    waterfallPlot(dataWithGroupsMap.entries.associate { (k, v) -> k to v.subList(0, groupSize) },\n",
    "                  \"Accounts\", \"Values\", measure = \"Measure\", calcTotal = false),\n",
    "))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ad245ee8-c6f7-4e28-8cee-25c13691da1a",
   "metadata": {},
   "source": [
    "### Tooltips\n",
    "\n",
    "Tooltips for relative and absolute measures should be specified independently."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "72425275-b5da-41e0-84d3-bd2d1cf36e6a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "   <div id=\"hF1BmA\"></div>\n",
       "   <script type=\"text/javascript\" data-lets-plot-script=\"plot\">\n",
       "       (function() {\n",
       "           var plotSpec={\n",
       "\"layout\":{\n",
       "\"name\":\"grid\",\n",
       "\"ncol\":2,\n",
       "\"nrow\":1,\n",
       "\"fit\":true,\n",
       "\"align\":false\n",
       "},\n",
       "\"figures\":[{\n",
       "\"ggtitle\":{\n",
       "\"text\":\"'detailed' tooltips\"\n",
       "},\n",
       "\"mapping\":{\n",
       "},\n",
       "\"data\":{\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[{\n",
       "\"aesthetic\":\"x\",\n",
       "\"name\":\"Accounts\",\n",
       "\"breaks\":[0.0,1.0,2.0,3.0,4.0],\n",
       "\"labels\":[\"Product revenue\",\"Services revenue\",\"Fixed costs\",\"Variable costs\",\"Total\"]\n",
       "},{\n",
       "\"aesthetic\":\"y\",\n",
       "\"name\":\"Values\"\n",
       "},{\n",
       "\"aesthetic\":\"color\",\n",
       "\"name\":\"Flow type\",\n",
       "\"breaks\":[\"Increase\",\"Decrease\",\"Total\"],\n",
       "\"values\":[\"#4daf4a\",\"#e41a1c\",\"#377eb8\"]\n",
       "},{\n",
       "\"aesthetic\":\"fill\",\n",
       "\"name\":\"Flow type\",\n",
       "\"breaks\":[\"Increase\",\"Decrease\",\"Total\"],\n",
       "\"values\":[\"#4daf4a\",\"#e41a1c\",\"#377eb8\"]\n",
       "}],\n",
       "\"layers\":[{\n",
       "\"geom\":\"spoke\",\n",
       "\"data\":{\n",
       "\"..radius..\":[0.09999999999999998,0.09999999999999998,0.09999999999999998,0.09999999999999998,0.0],\n",
       "\"..x..\":[0.0,1.0,2.0,3.0,4.0],\n",
       "\"..y..\":[830000.0,1120000.0,760000.0,610000.0,610000.0]\n",
       "},\n",
       "\"mapping\":{\n",
       "\"x\":\"..x..\",\n",
       "\"y\":\"..y..\",\n",
       "\"radius\":\"..radius..\"\n",
       "},\n",
       "\"angle\":0.0,\n",
       "\"position\":{\n",
       "\"name\":\"nudge\",\n",
       "\"x\":0.45\n",
       "}\n",
       "},{\n",
       "\"geom\":\"crossbar\",\n",
       "\"data\":{\n",
       "\"..value..\":[830000.0,1120000.0,760000.0,610000.0,610000.0],\n",
       "\"..flow_type..\":[\"Increase\",\"Increase\",\"Decrease\",\"Decrease\",\"Total\"],\n",
       "\"..dy..\":[830000.0,290000.0,-360000.0,-150000.0,610000.0],\n",
       "\"..x..\":[0.0,1.0,2.0,3.0,4.0],\n",
       "\"..ymax..\":[830000.0,1120000.0,1120000.0,760000.0,null],\n",
       "\"..initial..\":[0.0,830000.0,1120000.0,760000.0,0.0],\n",
       "\"..ymin..\":[0.0,830000.0,760000.0,610000.0,null],\n",
       "\"..xlabel..\":[\"Product revenue\",\"Services revenue\",\"Fixed costs\",\"Variable costs\",\"Total\"]\n",
       "},\n",
       "\"mapping\":{\n",
       "\"x\":\"..x..\",\n",
       "\"ymin\":\"..ymin..\",\n",
       "\"ymax\":\"..ymax..\",\n",
       "\"fill\":\"..flow_type..\"\n",
       "},\n",
       "\"color\":\"black\",\n",
       "\"size\":0.0,\n",
       "\"linetype\":1,\n",
       "\"width\":0.9,\n",
       "\"show_legend\":false,\n",
       "\"tooltips\":{\n",
       "\"title\":\"@..xlabel..\",\n",
       "\"disable_splitting\":true,\n",
       "\"lines\":[\"Initial|@..initial..\",\"Difference|@..dy..\",\"Cumulative sum|@..value..\"],\n",
       "\"formats\":[{\n",
       "\"field\":\"..initial..\",\n",
       "\"format\":\".2~f\"\n",
       "},{\n",
       "\"field\":\"..dy..\",\n",
       "\"format\":\".2~f\"\n",
       "},{\n",
       "\"field\":\"..value..\",\n",
       "\"format\":\".2~f\"\n",
       "}]\n",
       "}\n",
       "},{\n",
       "\"geom\":\"crossbar\",\n",
       "\"data\":{\n",
       "\"..value..\":[830000.0,1120000.0,760000.0,610000.0,610000.0],\n",
       "\"..flow_type..\":[\"Increase\",\"Increase\",\"Decrease\",\"Decrease\",\"Total\"],\n",
       "\"..x..\":[0.0,1.0,2.0,3.0,4.0],\n",
       "\"..ymax..\":[null,null,null,null,610000.0],\n",
       "\"..ymin..\":[null,null,null,null,0.0],\n",
       "\"..xlabel..\":[\"Product revenue\",\"Services revenue\",\"Fixed costs\",\"Variable costs\",\"Total\"]\n",
       "},\n",
       "\"mapping\":{\n",
       "\"x\":\"..x..\",\n",
       "\"ymin\":\"..ymin..\",\n",
       "\"ymax\":\"..ymax..\",\n",
       "\"fill\":\"..flow_type..\"\n",
       "},\n",
       "\"color\":\"black\",\n",
       "\"size\":0.0,\n",
       "\"linetype\":1,\n",
       "\"width\":0.9,\n",
       "\"show_legend\":false,\n",
       "\"tooltips\":{\n",
       "\"title\":\"@..xlabel..\",\n",
       "\"disable_splitting\":true,\n",
       "\"lines\":[\"Value|@..value..\"],\n",
       "\"formats\":[{\n",
       "\"field\":\"..value..\",\n",
       "\"format\":\".2~f\"\n",
       "}]\n",
       "}\n",
       "},{\n",
       "\"geom\":\"text\",\n",
       "\"data\":{\n",
       "\"..x..\":[0.0,1.0,2.0,3.0,4.0],\n",
       "\"..y..\":[415000.0,975000.0,940000.0,685000.0,305000.0],\n",
       "\"..label..\":[830000.0,290000.0,-360000.0,-150000.0,610000.0]\n",
       "},\n",
       "\"mapping\":{\n",
       "\"x\":\"..x..\",\n",
       "\"y\":\"..y..\",\n",
       "\"label\":\"..label..\"\n",
       "},\n",
       "\"color\":\"white\",\n",
       "\"show_legend\":false,\n",
       "\"label_format\":\".2~f\"\n",
       "}],\n",
       "\"data_meta\":{\n",
       "\"series_annotations\":[{\n",
       "\"type\":\"str\",\n",
       "\"column\":\"Accounts\"\n",
       "},{\n",
       "\"type\":\"int\",\n",
       "\"column\":\"Values\"\n",
       "}]\n",
       "},\n",
       "\"theme\":{\n",
       "\"axis_tooltip\":\"blank\"\n",
       "}\n",
       "},{\n",
       "\"ggtitle\":{\n",
       "\"text\":\"Custom tooltips\"\n",
       "},\n",
       "\"mapping\":{\n",
       "},\n",
       "\"data\":{\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[{\n",
       "\"aesthetic\":\"x\",\n",
       "\"name\":\"Accounts\",\n",
       "\"breaks\":[0.0,1.0,2.0,3.0,4.0],\n",
       "\"labels\":[\"Product revenue\",\"Services revenue\",\"Fixed costs\",\"Variable costs\",\"Total\"]\n",
       "},{\n",
       "\"aesthetic\":\"y\",\n",
       "\"name\":\"Values\"\n",
       "},{\n",
       "\"aesthetic\":\"color\",\n",
       "\"name\":\"Flow type\",\n",
       "\"breaks\":[\"Increase\",\"Decrease\",\"Total\"],\n",
       "\"values\":[\"#4daf4a\",\"#e41a1c\",\"#377eb8\"]\n",
       "},{\n",
       "\"aesthetic\":\"fill\",\n",
       "\"name\":\"Flow type\",\n",
       "\"breaks\":[\"Increase\",\"Decrease\",\"Total\"],\n",
       "\"values\":[\"#4daf4a\",\"#e41a1c\",\"#377eb8\"]\n",
       "}],\n",
       "\"layers\":[{\n",
       "\"geom\":\"spoke\",\n",
       "\"data\":{\n",
       "\"..radius..\":[0.09999999999999998,0.09999999999999998,0.09999999999999998,0.09999999999999998,0.0],\n",
       "\"..x..\":[0.0,1.0,2.0,3.0,4.0],\n",
       "\"..y..\":[830000.0,1120000.0,760000.0,610000.0,610000.0]\n",
       "},\n",
       "\"mapping\":{\n",
       "\"x\":\"..x..\",\n",
       "\"y\":\"..y..\",\n",
       "\"radius\":\"..radius..\"\n",
       "},\n",
       "\"angle\":0.0,\n",
       "\"position\":{\n",
       "\"name\":\"nudge\",\n",
       "\"x\":0.45\n",
       "}\n",
       "},{\n",
       "\"geom\":\"crossbar\",\n",
       "\"data\":{\n",
       "\"..value..\":[830000.0,1120000.0,760000.0,610000.0,610000.0],\n",
       "\"..flow_type..\":[\"Increase\",\"Increase\",\"Decrease\",\"Decrease\",\"Total\"],\n",
       "\"..x..\":[0.0,1.0,2.0,3.0,4.0],\n",
       "\"..ymax..\":[830000.0,1120000.0,1120000.0,760000.0,null],\n",
       "\"..initial..\":[0.0,830000.0,1120000.0,760000.0,0.0],\n",
       "\"..ymin..\":[0.0,830000.0,760000.0,610000.0,null],\n",
       "\"..xlabel..\":[\"Product revenue\",\"Services revenue\",\"Fixed costs\",\"Variable costs\",\"Total\"]\n",
       "},\n",
       "\"mapping\":{\n",
       "\"x\":\"..x..\",\n",
       "\"ymin\":\"..ymin..\",\n",
       "\"ymax\":\"..ymax..\",\n",
       "\"fill\":\"..flow_type..\"\n",
       "},\n",
       "\"color\":\"black\",\n",
       "\"size\":0.0,\n",
       "\"linetype\":1,\n",
       "\"width\":0.9,\n",
       "\"show_legend\":false,\n",
       "\"tooltips\":{\n",
       "\"title\":\"Account: @..xlabel..\",\n",
       "\"disable_splitting\":true,\n",
       "\"lines\":[\"@{..flow_type..}d from @..initial.. to @..value..\"],\n",
       "\"formats\":[{\n",
       "\"field\":\"@..initial..\",\n",
       "\"format\":\" $,.3~s\"\n",
       "},{\n",
       "\"field\":\"@..value..\",\n",
       "\"format\":\" $,.3~s\"\n",
       "}]\n",
       "}\n",
       "},{\n",
       "\"geom\":\"crossbar\",\n",
       "\"data\":{\n",
       "\"..flow_type..\":[\"Increase\",\"Increase\",\"Decrease\",\"Decrease\",\"Total\"],\n",
       "\"..x..\":[0.0,1.0,2.0,3.0,4.0],\n",
       "\"..ymax..\":[null,null,null,null,610000.0],\n",
       "\"..ymin..\":[null,null,null,null,0.0]\n",
       "},\n",
       "\"mapping\":{\n",
       "\"x\":\"..x..\",\n",
       "\"ymin\":\"..ymin..\",\n",
       "\"ymax\":\"..ymax..\",\n",
       "\"fill\":\"..flow_type..\"\n",
       "},\n",
       "\"color\":\"black\",\n",
       "\"size\":0.0,\n",
       "\"linetype\":1,\n",
       "\"width\":0.9,\n",
       "\"show_legend\":false,\n",
       "\"tooltips\":\"none\"\n",
       "},{\n",
       "\"geom\":\"text\",\n",
       "\"data\":{\n",
       "\"..x..\":[0.0,1.0,2.0,3.0,4.0],\n",
       "\"..y..\":[415000.0,975000.0,940000.0,685000.0,305000.0],\n",
       "\"..label..\":[830000.0,290000.0,-360000.0,-150000.0,610000.0]\n",
       "},\n",
       "\"mapping\":{\n",
       "\"x\":\"..x..\",\n",
       "\"y\":\"..y..\",\n",
       "\"label\":\"..label..\"\n",
       "},\n",
       "\"color\":\"white\",\n",
       "\"show_legend\":false,\n",
       "\"label_format\":\".2~f\"\n",
       "}],\n",
       "\"data_meta\":{\n",
       "\"series_annotations\":[{\n",
       "\"type\":\"str\",\n",
       "\"column\":\"Accounts\"\n",
       "},{\n",
       "\"type\":\"int\",\n",
       "\"column\":\"Values\"\n",
       "}]\n",
       "},\n",
       "\"theme\":{\n",
       "\"axis_tooltip\":\"blank\"\n",
       "}\n",
       "}],\n",
       "\"kind\":\"subplots\"\n",
       "};\n",
       "           var plotContainer = document.getElementById(\"hF1BmA\");\n",
       "           window.letsPlotCall(function() {{\n",
       "               LetsPlot.buildPlotFromProcessedSpecs(plotSpec, -1, -1, plotContainer);\n",
       "           }});\n",
       "       })();\n",
       "   </script>"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "val relativeTooltips = layerTooltips()\n",
    "    .title(\"Account: @..xlabel..\")\n",
    "    .format(\"@..initial..\", \" $,.3~s\")\n",
    "    .format(\"@..value..\", \" $,.3~s\")\n",
    "    .line(\"@{..flow_type..}d from @..initial.. to @..value..\")\n",
    "    .disableSplitting()\n",
    "\n",
    "gggrid(listOf(\n",
    "    waterfallPlot(dataMap, \"Accounts\", \"Values\", relativeTooltips = \"detailed\", absoluteTooltips = \"detailed\") + ggtitle(\"'detailed' tooltips\"),\n",
    "    waterfallPlot(dataMap, \"Accounts\", \"Values\", relativeTooltips = relativeTooltips, absoluteTooltips = \"none\") + ggtitle(\"Custom tooltips\"),\n",
    "))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "747094ce-a7a4-4ce9-b3f1-0794f3436cf9",
   "metadata": {},
   "source": [
    "### `sortedValue`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "2484b0c4-0389-4bd2-b8d2-f78af0bc15bb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "   <div id=\"IFvudQ\"></div>\n",
       "   <script type=\"text/javascript\" data-lets-plot-script=\"plot\">\n",
       "       (function() {\n",
       "           var plotSpec={\n",
       "\"mapping\":{\n",
       "},\n",
       "\"data\":{\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[{\n",
       "\"aesthetic\":\"x\",\n",
       "\"name\":\"Accounts\",\n",
       "\"breaks\":[0.0,1.0,2.0,3.0,4.0],\n",
       "\"labels\":[\"Product revenue\",\"Fixed costs\",\"Services revenue\",\"Variable costs\",\"Total\"]\n",
       "},{\n",
       "\"aesthetic\":\"y\",\n",
       "\"name\":\"Values\"\n",
       "},{\n",
       "\"aesthetic\":\"color\",\n",
       "\"name\":\"Flow type\",\n",
       "\"breaks\":[\"Increase\",\"Decrease\",\"Total\"],\n",
       "\"values\":[\"#4daf4a\",\"#e41a1c\",\"#377eb8\"]\n",
       "},{\n",
       "\"aesthetic\":\"fill\",\n",
       "\"name\":\"Flow type\",\n",
       "\"breaks\":[\"Increase\",\"Decrease\",\"Total\"],\n",
       "\"values\":[\"#4daf4a\",\"#e41a1c\",\"#377eb8\"]\n",
       "}],\n",
       "\"layers\":[{\n",
       "\"geom\":\"spoke\",\n",
       "\"data\":{\n",
       "\"..radius..\":[0.09999999999999998,0.09999999999999998,0.09999999999999998,0.09999999999999998,0.0],\n",
       "\"..x..\":[0.0,1.0,2.0,3.0,4.0],\n",
       "\"..y..\":[830000.0,470000.0,760000.0,610000.0,610000.0]\n",
       "},\n",
       "\"mapping\":{\n",
       "\"x\":\"..x..\",\n",
       "\"y\":\"..y..\",\n",
       "\"radius\":\"..radius..\"\n",
       "},\n",
       "\"angle\":0.0,\n",
       "\"position\":{\n",
       "\"name\":\"nudge\",\n",
       "\"x\":0.45\n",
       "}\n",
       "},{\n",
       "\"geom\":\"crossbar\",\n",
       "\"data\":{\n",
       "\"..flow_type..\":[\"Increase\",\"Decrease\",\"Increase\",\"Decrease\",\"Total\"],\n",
       "\"..dy..\":[830000.0,-360000.0,290000.0,-150000.0,610000.0],\n",
       "\"..x..\":[0.0,1.0,2.0,3.0,4.0],\n",
       "\"..ymax..\":[830000.0,830000.0,760000.0,760000.0,null],\n",
       "\"..ymin..\":[0.0,470000.0,470000.0,610000.0,null]\n",
       "},\n",
       "\"mapping\":{\n",
       "\"x\":\"..x..\",\n",
       "\"ymin\":\"..ymin..\",\n",
       "\"ymax\":\"..ymax..\",\n",
       "\"fill\":\"..flow_type..\"\n",
       "},\n",
       "\"color\":\"black\",\n",
       "\"size\":0.0,\n",
       "\"linetype\":1,\n",
       "\"width\":0.9,\n",
       "\"show_legend\":false,\n",
       "\"tooltips\":{\n",
       "\"lines\":[\"@..dy..\"],\n",
       "\"formats\":[{\n",
       "\"field\":\"..dy..\",\n",
       "\"format\":\".2~f\"\n",
       "}]\n",
       "}\n",
       "},{\n",
       "\"geom\":\"crossbar\",\n",
       "\"data\":{\n",
       "\"..value..\":[830000.0,470000.0,760000.0,610000.0,610000.0],\n",
       "\"..flow_type..\":[\"Increase\",\"Decrease\",\"Increase\",\"Decrease\",\"Total\"],\n",
       "\"..x..\":[0.0,1.0,2.0,3.0,4.0],\n",
       "\"..ymax..\":[null,null,null,null,610000.0],\n",
       "\"..ymin..\":[null,null,null,null,0.0]\n",
       "},\n",
       "\"mapping\":{\n",
       "\"x\":\"..x..\",\n",
       "\"ymin\":\"..ymin..\",\n",
       "\"ymax\":\"..ymax..\",\n",
       "\"fill\":\"..flow_type..\"\n",
       "},\n",
       "\"color\":\"black\",\n",
       "\"size\":0.0,\n",
       "\"linetype\":1,\n",
       "\"width\":0.9,\n",
       "\"show_legend\":false,\n",
       "\"tooltips\":{\n",
       "\"disable_splitting\":true,\n",
       "\"lines\":[\"@..value..\"],\n",
       "\"formats\":[{\n",
       "\"field\":\"..value..\",\n",
       "\"format\":\".2~f\"\n",
       "}]\n",
       "}\n",
       "},{\n",
       "\"geom\":\"text\",\n",
       "\"data\":{\n",
       "\"..x..\":[0.0,1.0,2.0,3.0,4.0],\n",
       "\"..y..\":[415000.0,650000.0,615000.0,685000.0,305000.0],\n",
       "\"..label..\":[830000.0,-360000.0,290000.0,-150000.0,610000.0]\n",
       "},\n",
       "\"mapping\":{\n",
       "\"x\":\"..x..\",\n",
       "\"y\":\"..y..\",\n",
       "\"label\":\"..label..\"\n",
       "},\n",
       "\"color\":\"white\",\n",
       "\"show_legend\":false,\n",
       "\"label_format\":\".2~f\"\n",
       "}],\n",
       "\"data_meta\":{\n",
       "\"series_annotations\":[{\n",
       "\"type\":\"str\",\n",
       "\"column\":\"Accounts\"\n",
       "},{\n",
       "\"type\":\"int\",\n",
       "\"column\":\"Values\"\n",
       "}]\n",
       "},\n",
       "\"theme\":{\n",
       "\"axis_tooltip\":\"blank\"\n",
       "}\n",
       "};\n",
       "           var plotContainer = document.getElementById(\"IFvudQ\");\n",
       "           window.letsPlotCall(function() {{\n",
       "               LetsPlot.buildPlotFromProcessedSpecs(plotSpec, -1, -1, plotContainer);\n",
       "           }});\n",
       "       })();\n",
       "   </script>"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "waterfallPlot(dataMap, \"Accounts\", \"Values\", sortedValue = true)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "047022ef-8363-4363-ae61-e4441b518c12",
   "metadata": {},
   "source": [
    "### `threshold`/`maxValues`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "86a9457e-98de-4800-8f5a-ad1860722685",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "   <div id=\"VlsTIc\"></div>\n",
       "   <script type=\"text/javascript\" data-lets-plot-script=\"plot\">\n",
       "       (function() {\n",
       "           var plotSpec={\n",
       "\"layout\":{\n",
       "\"name\":\"grid\",\n",
       "\"ncol\":3,\n",
       "\"nrow\":1,\n",
       "\"fit\":true,\n",
       "\"align\":false\n",
       "},\n",
       "\"figures\":[{\n",
       "\"ggtitle\":{\n",
       "\"text\":\"Default\"\n",
       "},\n",
       "\"mapping\":{\n",
       "},\n",
       "\"data\":{\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[{\n",
       "\"aesthetic\":\"x\",\n",
       "\"name\":\"Accounts\",\n",
       "\"breaks\":[0.0,1.0,2.0,3.0,4.0],\n",
       "\"labels\":[\"Product revenue\",\"Services revenue\",\"Fixed costs\",\"Variable costs\",\"Total\"]\n",
       "},{\n",
       "\"aesthetic\":\"y\",\n",
       "\"name\":\"Values\"\n",
       "},{\n",
       "\"aesthetic\":\"color\",\n",
       "\"name\":\"Flow type\",\n",
       "\"breaks\":[\"Increase\",\"Decrease\",\"Total\"],\n",
       "\"values\":[\"#4daf4a\",\"#e41a1c\",\"#377eb8\"]\n",
       "},{\n",
       "\"aesthetic\":\"fill\",\n",
       "\"name\":\"Flow type\",\n",
       "\"breaks\":[\"Increase\",\"Decrease\",\"Total\"],\n",
       "\"values\":[\"#4daf4a\",\"#e41a1c\",\"#377eb8\"]\n",
       "}],\n",
       "\"layers\":[{\n",
       "\"geom\":\"spoke\",\n",
       "\"data\":{\n",
       "\"..radius..\":[0.09999999999999998,0.09999999999999998,0.09999999999999998,0.09999999999999998,0.0],\n",
       "\"..x..\":[0.0,1.0,2.0,3.0,4.0],\n",
       "\"..y..\":[830000.0,1120000.0,760000.0,610000.0,610000.0]\n",
       "},\n",
       "\"mapping\":{\n",
       "\"x\":\"..x..\",\n",
       "\"y\":\"..y..\",\n",
       "\"radius\":\"..radius..\"\n",
       "},\n",
       "\"angle\":0.0,\n",
       "\"position\":{\n",
       "\"name\":\"nudge\",\n",
       "\"x\":0.45\n",
       "}\n",
       "},{\n",
       "\"geom\":\"crossbar\",\n",
       "\"data\":{\n",
       "\"..flow_type..\":[\"Increase\",\"Increase\",\"Decrease\",\"Decrease\",\"Total\"],\n",
       "\"..dy..\":[830000.0,290000.0,-360000.0,-150000.0,610000.0],\n",
       "\"..x..\":[0.0,1.0,2.0,3.0,4.0],\n",
       "\"..ymax..\":[830000.0,1120000.0,1120000.0,760000.0,null],\n",
       "\"..ymin..\":[0.0,830000.0,760000.0,610000.0,null]\n",
       "},\n",
       "\"mapping\":{\n",
       "\"x\":\"..x..\",\n",
       "\"ymin\":\"..ymin..\",\n",
       "\"ymax\":\"..ymax..\",\n",
       "\"fill\":\"..flow_type..\"\n",
       "},\n",
       "\"color\":\"black\",\n",
       "\"size\":0.0,\n",
       "\"linetype\":1,\n",
       "\"width\":0.9,\n",
       "\"show_legend\":false,\n",
       "\"tooltips\":{\n",
       "\"lines\":[\"@..dy..\"],\n",
       "\"formats\":[{\n",
       "\"field\":\"..dy..\",\n",
       "\"format\":\".2~f\"\n",
       "}]\n",
       "}\n",
       "},{\n",
       "\"geom\":\"crossbar\",\n",
       "\"data\":{\n",
       "\"..value..\":[830000.0,1120000.0,760000.0,610000.0,610000.0],\n",
       "\"..flow_type..\":[\"Increase\",\"Increase\",\"Decrease\",\"Decrease\",\"Total\"],\n",
       "\"..x..\":[0.0,1.0,2.0,3.0,4.0],\n",
       "\"..ymax..\":[null,null,null,null,610000.0],\n",
       "\"..ymin..\":[null,null,null,null,0.0]\n",
       "},\n",
       "\"mapping\":{\n",
       "\"x\":\"..x..\",\n",
       "\"ymin\":\"..ymin..\",\n",
       "\"ymax\":\"..ymax..\",\n",
       "\"fill\":\"..flow_type..\"\n",
       "},\n",
       "\"color\":\"black\",\n",
       "\"size\":0.0,\n",
       "\"linetype\":1,\n",
       "\"width\":0.9,\n",
       "\"show_legend\":false,\n",
       "\"tooltips\":{\n",
       "\"disable_splitting\":true,\n",
       "\"lines\":[\"@..value..\"],\n",
       "\"formats\":[{\n",
       "\"field\":\"..value..\",\n",
       "\"format\":\".2~f\"\n",
       "}]\n",
       "}\n",
       "},{\n",
       "\"geom\":\"text\",\n",
       "\"data\":{\n",
       "\"..x..\":[0.0,1.0,2.0,3.0,4.0],\n",
       "\"..y..\":[415000.0,975000.0,940000.0,685000.0,305000.0],\n",
       "\"..label..\":[830000.0,290000.0,-360000.0,-150000.0,610000.0]\n",
       "},\n",
       "\"mapping\":{\n",
       "\"x\":\"..x..\",\n",
       "\"y\":\"..y..\",\n",
       "\"label\":\"..label..\"\n",
       "},\n",
       "\"color\":\"white\",\n",
       "\"show_legend\":false,\n",
       "\"label_format\":\".2~f\"\n",
       "}],\n",
       "\"data_meta\":{\n",
       "\"series_annotations\":[{\n",
       "\"type\":\"str\",\n",
       "\"column\":\"Accounts\"\n",
       "},{\n",
       "\"type\":\"int\",\n",
       "\"column\":\"Values\"\n",
       "}]\n",
       "},\n",
       "\"theme\":{\n",
       "\"axis_tooltip\":\"blank\"\n",
       "}\n",
       "},{\n",
       "\"ggtitle\":{\n",
       "\"text\":\"Specified threshold\"\n",
       "},\n",
       "\"mapping\":{\n",
       "},\n",
       "\"data\":{\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[{\n",
       "\"aesthetic\":\"x\",\n",
       "\"name\":\"Accounts\",\n",
       "\"breaks\":[0.0,1.0,2.0,3.0],\n",
       "\"labels\":[\"Product revenue\",\"Fixed costs\",\"Other\",\"Total\"]\n",
       "},{\n",
       "\"aesthetic\":\"y\",\n",
       "\"name\":\"Values\"\n",
       "},{\n",
       "\"aesthetic\":\"color\",\n",
       "\"name\":\"Flow type\",\n",
       "\"breaks\":[\"Increase\",\"Decrease\",\"Total\"],\n",
       "\"values\":[\"#4daf4a\",\"#e41a1c\",\"#377eb8\"]\n",
       "},{\n",
       "\"aesthetic\":\"fill\",\n",
       "\"name\":\"Flow type\",\n",
       "\"breaks\":[\"Increase\",\"Decrease\",\"Total\"],\n",
       "\"values\":[\"#4daf4a\",\"#e41a1c\",\"#377eb8\"]\n",
       "}],\n",
       "\"layers\":[{\n",
       "\"geom\":\"spoke\",\n",
       "\"data\":{\n",
       "\"..radius..\":[0.09999999999999998,0.09999999999999998,0.09999999999999998,0.0],\n",
       "\"..x..\":[0.0,1.0,2.0,3.0],\n",
       "\"..y..\":[830000.0,470000.0,610000.0,610000.0]\n",
       "},\n",
       "\"mapping\":{\n",
       "\"x\":\"..x..\",\n",
       "\"y\":\"..y..\",\n",
       "\"radius\":\"..radius..\"\n",
       "},\n",
       "\"angle\":0.0,\n",
       "\"position\":{\n",
       "\"name\":\"nudge\",\n",
       "\"x\":0.45\n",
       "}\n",
       "},{\n",
       "\"geom\":\"crossbar\",\n",
       "\"data\":{\n",
       "\"..flow_type..\":[\"Increase\",\"Decrease\",\"Increase\",\"Total\"],\n",
       "\"..dy..\":[830000.0,-360000.0,140000.0,610000.0],\n",
       "\"..x..\":[0.0,1.0,2.0,3.0],\n",
       "\"..ymax..\":[830000.0,830000.0,610000.0,null],\n",
       "\"..ymin..\":[0.0,470000.0,470000.0,null]\n",
       "},\n",
       "\"mapping\":{\n",
       "\"x\":\"..x..\",\n",
       "\"ymin\":\"..ymin..\",\n",
       "\"ymax\":\"..ymax..\",\n",
       "\"fill\":\"..flow_type..\"\n",
       "},\n",
       "\"color\":\"black\",\n",
       "\"size\":0.0,\n",
       "\"linetype\":1,\n",
       "\"width\":0.9,\n",
       "\"show_legend\":false,\n",
       "\"tooltips\":{\n",
       "\"lines\":[\"@..dy..\"],\n",
       "\"formats\":[{\n",
       "\"field\":\"..dy..\",\n",
       "\"format\":\".2~f\"\n",
       "}]\n",
       "}\n",
       "},{\n",
       "\"geom\":\"crossbar\",\n",
       "\"data\":{\n",
       "\"..value..\":[830000.0,470000.0,610000.0,610000.0],\n",
       "\"..flow_type..\":[\"Increase\",\"Decrease\",\"Increase\",\"Total\"],\n",
       "\"..x..\":[0.0,1.0,2.0,3.0],\n",
       "\"..ymax..\":[null,null,null,610000.0],\n",
       "\"..ymin..\":[null,null,null,0.0]\n",
       "},\n",
       "\"mapping\":{\n",
       "\"x\":\"..x..\",\n",
       "\"ymin\":\"..ymin..\",\n",
       "\"ymax\":\"..ymax..\",\n",
       "\"fill\":\"..flow_type..\"\n",
       "},\n",
       "\"color\":\"black\",\n",
       "\"size\":0.0,\n",
       "\"linetype\":1,\n",
       "\"width\":0.9,\n",
       "\"show_legend\":false,\n",
       "\"tooltips\":{\n",
       "\"disable_splitting\":true,\n",
       "\"lines\":[\"@..value..\"],\n",
       "\"formats\":[{\n",
       "\"field\":\"..value..\",\n",
       "\"format\":\".2~f\"\n",
       "}]\n",
       "}\n",
       "},{\n",
       "\"geom\":\"text\",\n",
       "\"data\":{\n",
       "\"..x..\":[0.0,1.0,2.0,3.0],\n",
       "\"..y..\":[415000.0,650000.0,540000.0,305000.0],\n",
       "\"..label..\":[830000.0,-360000.0,140000.0,610000.0]\n",
       "},\n",
       "\"mapping\":{\n",
       "\"x\":\"..x..\",\n",
       "\"y\":\"..y..\",\n",
       "\"label\":\"..label..\"\n",
       "},\n",
       "\"color\":\"white\",\n",
       "\"show_legend\":false,\n",
       "\"label_format\":\".2~f\"\n",
       "}],\n",
       "\"data_meta\":{\n",
       "\"series_annotations\":[{\n",
       "\"type\":\"str\",\n",
       "\"column\":\"Accounts\"\n",
       "},{\n",
       "\"type\":\"int\",\n",
       "\"column\":\"Values\"\n",
       "}]\n",
       "},\n",
       "\"theme\":{\n",
       "\"axis_tooltip\":\"blank\"\n",
       "}\n",
       "},{\n",
       "\"ggtitle\":{\n",
       "\"text\":\"Specified maxValues\"\n",
       "},\n",
       "\"mapping\":{\n",
       "},\n",
       "\"data\":{\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[{\n",
       "\"aesthetic\":\"x\",\n",
       "\"name\":\"Accounts\",\n",
       "\"breaks\":[0.0,1.0,2.0,3.0],\n",
       "\"labels\":[\"Product revenue\",\"Fixed costs\",\"Other\",\"Total\"]\n",
       "},{\n",
       "\"aesthetic\":\"y\",\n",
       "\"name\":\"Values\"\n",
       "},{\n",
       "\"aesthetic\":\"color\",\n",
       "\"name\":\"Flow type\",\n",
       "\"breaks\":[\"Increase\",\"Decrease\",\"Total\"],\n",
       "\"values\":[\"#4daf4a\",\"#e41a1c\",\"#377eb8\"]\n",
       "},{\n",
       "\"aesthetic\":\"fill\",\n",
       "\"name\":\"Flow type\",\n",
       "\"breaks\":[\"Increase\",\"Decrease\",\"Total\"],\n",
       "\"values\":[\"#4daf4a\",\"#e41a1c\",\"#377eb8\"]\n",
       "}],\n",
       "\"layers\":[{\n",
       "\"geom\":\"spoke\",\n",
       "\"data\":{\n",
       "\"..radius..\":[0.09999999999999998,0.09999999999999998,0.09999999999999998,0.0],\n",
       "\"..x..\":[0.0,1.0,2.0,3.0],\n",
       "\"..y..\":[830000.0,470000.0,610000.0,610000.0]\n",
       "},\n",
       "\"mapping\":{\n",
       "\"x\":\"..x..\",\n",
       "\"y\":\"..y..\",\n",
       "\"radius\":\"..radius..\"\n",
       "},\n",
       "\"angle\":0.0,\n",
       "\"position\":{\n",
       "\"name\":\"nudge\",\n",
       "\"x\":0.45\n",
       "}\n",
       "},{\n",
       "\"geom\":\"crossbar\",\n",
       "\"data\":{\n",
       "\"..flow_type..\":[\"Increase\",\"Decrease\",\"Increase\",\"Total\"],\n",
       "\"..dy..\":[830000.0,-360000.0,140000.0,610000.0],\n",
       "\"..x..\":[0.0,1.0,2.0,3.0],\n",
       "\"..ymax..\":[830000.0,830000.0,610000.0,null],\n",
       "\"..ymin..\":[0.0,470000.0,470000.0,null]\n",
       "},\n",
       "\"mapping\":{\n",
       "\"x\":\"..x..\",\n",
       "\"ymin\":\"..ymin..\",\n",
       "\"ymax\":\"..ymax..\",\n",
       "\"fill\":\"..flow_type..\"\n",
       "},\n",
       "\"color\":\"black\",\n",
       "\"size\":0.0,\n",
       "\"linetype\":1,\n",
       "\"width\":0.9,\n",
       "\"show_legend\":false,\n",
       "\"tooltips\":{\n",
       "\"lines\":[\"@..dy..\"],\n",
       "\"formats\":[{\n",
       "\"field\":\"..dy..\",\n",
       "\"format\":\".2~f\"\n",
       "}]\n",
       "}\n",
       "},{\n",
       "\"geom\":\"crossbar\",\n",
       "\"data\":{\n",
       "\"..value..\":[830000.0,470000.0,610000.0,610000.0],\n",
       "\"..flow_type..\":[\"Increase\",\"Decrease\",\"Increase\",\"Total\"],\n",
       "\"..x..\":[0.0,1.0,2.0,3.0],\n",
       "\"..ymax..\":[null,null,null,610000.0],\n",
       "\"..ymin..\":[null,null,null,0.0]\n",
       "},\n",
       "\"mapping\":{\n",
       "\"x\":\"..x..\",\n",
       "\"ymin\":\"..ymin..\",\n",
       "\"ymax\":\"..ymax..\",\n",
       "\"fill\":\"..flow_type..\"\n",
       "},\n",
       "\"color\":\"black\",\n",
       "\"size\":0.0,\n",
       "\"linetype\":1,\n",
       "\"width\":0.9,\n",
       "\"show_legend\":false,\n",
       "\"tooltips\":{\n",
       "\"disable_splitting\":true,\n",
       "\"lines\":[\"@..value..\"],\n",
       "\"formats\":[{\n",
       "\"field\":\"..value..\",\n",
       "\"format\":\".2~f\"\n",
       "}]\n",
       "}\n",
       "},{\n",
       "\"geom\":\"text\",\n",
       "\"data\":{\n",
       "\"..x..\":[0.0,1.0,2.0,3.0],\n",
       "\"..y..\":[415000.0,650000.0,540000.0,305000.0],\n",
       "\"..label..\":[830000.0,-360000.0,140000.0,610000.0]\n",
       "},\n",
       "\"mapping\":{\n",
       "\"x\":\"..x..\",\n",
       "\"y\":\"..y..\",\n",
       "\"label\":\"..label..\"\n",
       "},\n",
       "\"color\":\"white\",\n",
       "\"show_legend\":false,\n",
       "\"label_format\":\".2~f\"\n",
       "}],\n",
       "\"data_meta\":{\n",
       "\"series_annotations\":[{\n",
       "\"type\":\"str\",\n",
       "\"column\":\"Accounts\"\n",
       "},{\n",
       "\"type\":\"int\",\n",
       "\"column\":\"Values\"\n",
       "}]\n",
       "},\n",
       "\"theme\":{\n",
       "\"axis_tooltip\":\"blank\"\n",
       "}\n",
       "}],\n",
       "\"kind\":\"subplots\"\n",
       "};\n",
       "           var plotContainer = document.getElementById(\"VlsTIc\");\n",
       "           window.letsPlotCall(function() {{\n",
       "               LetsPlot.buildPlotFromProcessedSpecs(plotSpec, -1, -1, plotContainer);\n",
       "           }});\n",
       "       })();\n",
       "   </script>"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gggrid(listOf(\n",
    "    waterfallPlot(dataMap, \"Accounts\", \"Values\") + ggtitle(\"Default\"),\n",
    "    waterfallPlot(dataMap, \"Accounts\", \"Values\", threshold = 300_000.0) + ggtitle(\"Specified threshold\"),\n",
    "    waterfallPlot(dataMap, \"Accounts\", \"Values\", maxValues = 2) + ggtitle(\"Specified maxValues\"),\n",
    "))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Kotlin",
   "language": "kotlin",
   "name": "kotlin"
  },
  "language_info": {
   "codemirror_mode": "text/x-kotlin",
   "file_extension": ".kt",
   "mimetype": "text/x-kotlin",
   "name": "kotlin",
   "nbconvert_exporter": "",
   "pygments_lexer": "kotlin",
   "version": "1.9.23"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
